﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._8
{

    public class Person : IComparable
    {
        public string Name;
        public int Age;
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
        public int CompareTo(object obj)
        {
            if (obj is Person)
            {
                Person otherPerson = obj as Person;
                return this.Age - otherPerson.Age;
            }
            else
            {
                throw new ArgumentException("Object to compare to is not a PERson objcet.");
            }
        }
    }

    public class PersonComparerName : IComparer
    {
        public static IComparer Default = new PersonComparerName();
        public int Compare(object x, object y)
        {
            if (x is Person && y is Person)
            {
                return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);
            }
            else
            {
                throw new ArgumentException("one or both objects to compare are not person objects.");
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(new Person("a1", 1));
            list.Add(new Person("z2", 2));
            list.Add(new Person("b3", 9));
            list.Add(new Person("f4", 6));
            list.Add(new Person("c5", 8));

            for (int i = 0; i < list.Count; i++)
            {

                Console.WriteLine((list[i] as Person).Name + ' ' + (list[i] as Person).Age.ToString());


            }
            Console.WriteLine();
            Console.WriteLine();
            list.Sort();
            for (int i = 0; i < list.Count; i++)
            {

                Console.WriteLine((list[i] as Person).Name + ' ' + (list[i] as Person).Age.ToString());


            }

            Console.WriteLine();
            Console.WriteLine();
            list.Sort(PersonComparerName.Default);
            for (int i = 0; i < list.Count; i++)
            {

                Console.WriteLine((list[i] as Person).Name + ' ' + (list[i] as Person).Age.ToString());


            }

            Console.ReadLine();

        }
    }
}

